/**
 * @Description:配置部门根节点
 * @Author: CHEHSHUANG
 * @Date: 2019/1/23
 */
import React, {Component} from 'react';
import {Button, Checkbox, Col, Form, Input, message, Row} from "antd";
import {saveDepartmentRoot} from "./TenantService";

const FormItem = Form.Item;
const formLayout = {
    labelCol: {span: 6},
    wrapperCol: {span: 16}
}

class ConfigDepartmentRoot extends Component {
    constructor(props) {
        super(props);
        this.state = {
            loading: false
        }
    }
    refreshRoot = (departmentRoot={}) => {
      this.props.form.setFieldsValue(departmentRoot)
    }
    handleSave = () => {
        let result = false;
        this.props.form.validateFieldsAndScroll((err, values) => {
            if (!err) {
                result = values;
            }
        });
        if(result){
            const {departmentRoot={},refresh,tenantCode} = this.props;
            let saveData;
            saveData = {...departmentRoot, ...result,tenantCode};
            this.setState({loading: true});
            saveDepartmentRoot(saveData).then(res => {
                if(res.status.toLowerCase()==="success"){
                    if(res&&res.data){
                      refresh(res.data||{});
                      this.refreshRoot(res.data||{})
                    }
                    message.success(res.message);
                }else{
                    message.error(res.message);
                }
            }).catch(e => {
            }).finally(() => {
                this.setState({loading: false});
            })
        }
    }
    render() {
        const {getFieldDecorator} = this.props.form;
        const {departmentRoot} = this.props;
        const {loading} = this.state;
        return (
            <Form>
                <FormItem
                    label={"代码"}
                    {...formLayout}
                >
                    {getFieldDecorator('code', {
                        initialValue: departmentRoot && departmentRoot.code ? departmentRoot.code : "",
                        rules: [{required: false}]
                    })(
                        <Input disabled/>
                    )}
                </FormItem>
                <FormItem
                    label={"名称"}
                    {...formLayout}
                >
                    {getFieldDecorator('name', {
                        initialValue: departmentRoot && departmentRoot.name ? departmentRoot.name : "",
                        rules: [{required: true, message: '请填写名称!'}]
                    })(
                        <Input placeholder={"请填写名称"}/>
                    )}
                </FormItem>
                <FormItem
                    label={"冻结"}
                    {...formLayout}
                >
                    {getFieldDecorator('frozen', {
                        initialValue: departmentRoot ? departmentRoot.frozen : false,
                        valuePropName: "checked"
                    })(
                        <Checkbox/>
                    )}
                </FormItem>
                <br/>
                <Row type={"flex"} justify={"center"} style={{marginTop: 20}}>
                    <Button
                        type={"primary"}
                        loading={loading}
                        onClick={this.handleSave}
                    >
                    保存
                    </Button>
                </Row>
            </Form>
        );
    }
}

export default Form.create()(ConfigDepartmentRoot);
